<?php

namespace app\api\model;

use app\common\library\Image;
use fast\Random;
use think\Model;


class Block extends Model
{

    // 表名
    protected $name = 'exam_paper_block';

    //题块分隔
    public static function cropBlock($exams_list_id, $student_id)
    {

        $blockList = db('exam_paper_block')->where('exams_list_id', $exams_list_id)->select();
        $paperList = db('exam_paper_images')->where(['exams_list_id' => $exams_list_id, 'student_id' => $student_id])->select();
        if (empty($blockList) || empty($paperList)) {
            return;
        }
        $data = [];
        foreach ($paperList as $key => $paper) {
            if (intval($paper['page']) == 1) {
                $crop = [];
                foreach ($blockList as $block) {
                    $content = json_decode($block['content'], true);
                    for ($i = 0; $i < count($content); $i++) {
                        if (intval($content[$i]['paperIndex']) == 1) {
                            $fileimage = pathinfo($paper['image']);
                            $imagePath = explode('.com', $fileimage['dirname']);
                            $image = $imagePath[1] . '/' . $fileimage['basename'];
                            $mairk = $imagePath[1];
                            $outputFile =  $mairk . '/' . md5(date('YmdHis', time()) . Random::uuid()) . '.png';
                            Image::cropImage($image, $outputFile, $content[$i]['width'], $content[$i]['height'], $content[$i]['left'], $content[$i]['top']);
                            $images = cdnurl($outputFile, true);
                            $crop[] = [
                                'id' => $paper['id'],
                                'block_id' => $block['id'],
                                'image' => $images
                            ];
                        }
                    }
                }
                $data = array_merge($data, $crop);
                db('exam_paper_images')->where(['id' => $paper['id']])->update([
                    'ispart' => 1
                ]);
            } else {
                $crop = [];
                foreach ($blockList as $block) {
                    $content = json_decode($block['content'], true);
                    for ($i = 0; $i < count($content); $i++) {
                        if (intval($content[$i]['paperIndex']) == 2) {
                            $fileimage = pathinfo($paper['image']);
                            $imagePath = explode('.com', $fileimage['dirname']);
                            $image = $imagePath[1] . '/' . $fileimage['basename'];
                            $mairk = $imagePath[1];
                            $outputFile =  $mairk . '/' . md5(date('YmdHis', time()) . Random::uuid()) . '.png';
                            Image::cropImage($image, $outputFile, $content[$i]['width'], $content[$i]['height'], $content[$i]['left'], $content[$i]['top']);
                            $images = cdnurl($outputFile, true);
                            $crop[] = [
                                'id' => $paper['id'],
                                'block_id' => $block['id'],
                                'image' => $images
                            ];
                        }
                    }
                } 
                $data = array_merge($data, $crop);
                db('exam_paper_images')->where(['id' => $paper['id']])->update([
                    'ispart' => 1
                ]);
            }
        }
        if(empty($data)) return;
        $temp = array_column($data, null, 'block_id');
        $newArray = self::arrayHandle($data, $temp);
        if(empty($newArray)) return;
        foreach ($newArray as $key => $val) {
            $array = [];
            foreach ($val as $k => &$v) {
                $imageArray = explode(',', $v['image']);
                $imageList = [];
                $mairk = '';
                foreach ($imageArray as $image) {
                    $fileimage = pathinfo($image);
                    $imagePath = explode('.com', $fileimage['dirname']);
                    $imageList[] = $imagePath[1] . '/' . $fileimage['basename'];
                    $mairk = $imagePath[1];
                }
                $outputFile =  $mairk . '/' . md5(date('YmdHis', time()) . Random::uuid()) . '.png';
                Image::generate($outputFile, "png", 1, $imageList);
                $v['mergeImage'] = cdnurl($outputFile, true);
                unset($v['id']);
                $array[] = $v;
            }

            db('exam_paper_images')->where(['id' => $key])->update([
                'zhudivide' => json_encode($val)
            ]);
        }

        return;
    }

    //数组处理 
    public static function arrayHandle($data, $temp)
    {
        $dataArr = [];
        foreach ($temp as $key => $value) {
            $image = [];
            $id = [];
            foreach ($data as $k => $val) {
                if ($key == $val['block_id']) {
                    $image[] = $val['image'];
                    $id[] = $val['id'];
                }
            }
            $dataArr[$key]['id'] = min($id);
            $dataArr[$key]['block_id'] = $key;
            $dataArr[$key]['image'] = implode(',', $image);
        }
        $res = []; //想要的结果
        foreach ($dataArr as $k => $v) {
            $res[$v['id']][] = $v;
        }

        return $res;
    }
}
